Thread: [Newbie] C++ call by reference question

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    16

    Lightbulb [Newbie] C++ call by reference question

    Hello!

    I have typedefs like the following:
    Code:
    typedef std::set<std::string>TBookKey;
    typedef std::vector<bookObjs>TBookValues;
    typedef std::map<bookKey, bookValues> TBookMap;
    And, member variable declaration:
    Code:
    private:
    TBookMap bookMap;
    ** Question **
    Is it possible to create an API to obtain:
    The pointer or reference to the TBookKey (set), and TBookValues (vector) of the a specific pair from the bookMap? (Not a copy of them)

    I think, if I define the API signature such as:
    Code:
    // iterate through BookMap to get the actual Key/Value reference of specified pair.
    
    void
    GetBookMapPair(const int bookID, TBookKey& key, TBookValues& value);
    
    The caller will always get a copy of TBookKey and TBookValues of the specified (bookID) pair, but not the pointer/reference of the Key/Value. Because that the operator= of set/vector is overloaded to make a copy. Is it correct? Any suggestions/comments?

    may tks!

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    No, you're not correct.

    The function GetBookMapPair() accepts two argum,ents (key and value) by reference. Those arguments are not copied.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by grumpy View Post
    No, you're not correct.

    The function GetBookMapPair() accepts two argum,ents (key and value) by reference. Those arguments are not copied.
    I *think* what he's asking about is to get a reference to the actual object in the set or vector, given the ID number of ... I'm not sure what. (In other words, can you force a set or a vector to give you a reference to an object that it contains, rather than a copy of it?)

    On the other hand, I'm not sure why we have a map from a set of strings to a vector of bookObjs in the first place, so there is quite a mystery.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by tabstop View Post
    I *think* what he's asking about is to get a reference to the actual object in the set or vector
    Maybe. In that case, yes it is possible. A function can return a reference. Some of the member functions of set and vector (including incantations of operator[]) do so.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Call by reference question
    By badtwistofate in forum C Programming
    Replies: 4
    Last Post: 03-24-2009, 02:40 PM
  2. call by reference question
    By staticalloc in forum C Programming
    Replies: 2
    Last Post: 09-09-2004, 12:58 PM
  3. Call-by-value Vs. Call-by-reference
    By Wiz_Nil in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2002, 09:06 AM
  4. call by reference and a call by value
    By IceCold in forum C Programming
    Replies: 4
    Last Post: 09-08-2001, 05:06 PM